package com.example.springboot_zc.controller;

import com.example.springboot_zc.pojo.*;
import com.example.springboot_zc.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

import java.awt.*;

import java.util.*;
import java.util.List;

@RestController
@RequestMapping("/login")
public class userController {

    @Autowired
    userService userService;

    @PostMapping("/findByName/{username}&{password}")
    public Boolean findByName(@PathVariable("username") String username, @PathVariable("password") String password) {

//        System.out.println(username);
//        System.out.println(userService.findName(username));
        if (password.equals(userService.findName(username).getPassword()))
            return true;
        return false;
    }

    @PostMapping("/findByName1/{username}")
    public User findByName1(@PathVariable("username") String username) {

//        System.out.println(username);
//        System.out.println(userService.findName(username));
        return userService.findName(username);
    }

    @PostMapping("/findStatus/{username}&{password}")
    public User findByS(@PathVariable("username") String username, @PathVariable("password") String password) {

//        System.out.println(username);
//        System.out.println(userService.findName(username));

        return userService.findName(username);
    }

    @PostMapping("/updataMine/{phone}&{address}&{username}")
    public Boolean updataMine(@PathVariable("phone") String phone,@PathVariable("address") String address,@PathVariable("username") String username){
        if (userService.updataMine(phone, address, username) != null)
            return true;
        return false;
    }

    @PostMapping("/updatapassword/{username}&{idcard}")
    public Boolean uppass(@PathVariable("username") String username, @PathVariable("idcard") String idcard) {
        if (userService.updatapassword(username, idcard) != null)
            return true;
        return false;
    }

    @PostMapping("/updatapass/{username}&{password}")
    public Integer uploadpass(@PathVariable("username") String username, @PathVariable("password") String password) {
        return userService.updataps(username, password);

    }

    @PostMapping("/l")
    public void createWord(@RequestBody Object o) {
        //创建Document对象
        Document doc = new Document();
        Section sec = doc.addSection();

        //声明数组内容
//        String[] header = {"班级","姓名","性别", "学号", "专业成绩"};
        String[][] data = {
                new String[]{"姓名：ikun", "", "班级：", "学号："},
                new String[]{"", "德育项目", "加、扣分事项明细", "得分"},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "德育基础分", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "德育互评分", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"德育测评", "德育素质加分", "", "0"},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "德育扣分", "", "0"},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "德育总分", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "智育项目", "附加分、扣分事项", "得分"},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "智育基础分", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "智育创新能力加分", "", "0"},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"智育测评", "智育扣分", "", "0"},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "", "", ""},
                new String[]{"", "德育总分", "", ""},
                new String[]{"", "", "", ""},
        };
//        data[0][1]=data[0][1]+o.ZyData.name;

        String s = o.toString();
//        System.out.println(s);
        String[] str = s.split("\\{");
        int flag1 = -1, flag2 = -1, i = 0;
        int[] num = {0, 0};
        for (String q : str) {
            if (flag1 < 1) {
                flag1++;
                continue;
            }
//            System.out.println(q);
            int y = q.indexOf('}');
            int a = q.substring(y + 1, q.length() - 1).indexOf("Data");
            num[i]++;
            if (a != -1) {
                i++;
            }
        }
        for (int j = 0; j < num.length; j++) {
            num[j] = num[j] * 10;
//            System.out.println(num[j]);
        }
        int zin = 0, din = 0;
        Boolean Zf = true, Df = true;

        int zxxx = 39, dxxx = 2, zzzz = 0, dddd = 0;
        String ttt = "", self = "", sug = "", bw = "";

        for (String q : str) {
            if (flag2 < 1) {
                flag2++;
                continue;
            }
            int y = q.indexOf('}');
            String p = q.substring(0, y + 1);
            p = '{' + p;
//            System.out.println(p);}
            Map<String, String> m = selfController.mapStringToMap(p);
            Set<String> keys = m.keySet();
//            for(String key:keys){
//                System.out.println("key: "+key+" value: "+m.get(key));
//            }

            for (String key : keys) {
//                System.out.println("key："+key+" value:"+m.get(key));
                if (num[0] > 0) {
                    if (key.equals("classNum"))
                        data[0][2] = "班级：" + m.get(key);
                    if (key.equals("stuNum"))
                        data[0][3] = "学号：" + m.get(key);
                    if (key.equals("contentSm"))
                        ttt = m.get(key);
                    if(key.equals("xxName")){
                        if (m.get(key).equals("智育基础分"))
                            zzzz = 1;
                        if (m.get(key).equals("智育附加分"))
                            zzzz = 2;
                    }
                    if (key.equals("sugScore"))
                        sug = m.get(key);
                    if (key.equals("zySelfScore"))
                        self = m.get(key);
                    num[0]--;
                    if (num[0] % 10 == 0) {
                        if (zzzz == 2) {
                            data[zxxx][2] = ttt + "  " + sug;
                            zxxx++;
                            if(Integer.valueOf(sug)>0)
                                data[49][3]=String.valueOf(Integer.valueOf(data[49][3])+Integer.valueOf(sug));
                            if(Integer.valueOf(sug)<0)
                                data[56][3]=String.valueOf(Integer.valueOf(data[56][3])+Integer.valueOf(sug));
                        }
                        if (zzzz == 1) {
                            data[42][3] = self;
                        }
                    }
                    continue;
                }

                if (num[1] > 0) {
                    if (key.equals("classNum"))
                        data[0][2] = "班级：" + m.get(key);
                    if (key.equals("stuNum"))
                        data[0][3] = "学号：" + m.get(key);
                    if (key.equals("contentSm"))
                        ttt = m.get(key);
                    if(key.equals("xxName")){
                        if (m.get(key).equals("德育基础分"))
                            dddd = 1;
                        if (m.get(key).equals("德育附加分"))
                            dddd = 2;
                    }
                    if (key.equals("sugScore"))
                        sug = m.get(key);
                    if (key.equals("dySelfScore"))
                        self = m.get(key);
                    if (key.equals("dyBwScore"))
                        bw = m.get(key);
                    num[1]--;
                    if (num[1] % 10 == 0) {
                        if (dddd == 2) {
                            data[dxxx][2] = ttt + "  " + sug;
                            dxxx++;
                            if(Integer.valueOf(sug)>0)
                                data[19][3]= String.valueOf(Integer.valueOf(data[19][3])+Integer.valueOf(sug));
                            if(Integer.valueOf(sug)<0)
                                data[26][3]=String.valueOf(Integer.valueOf(data[26][3])+Integer.valueOf(sug));
                        }
                        if (dddd == 1) {
                            data[5][3] = self;
                            data[12][3] = bw;
                        }
                    }
                    continue;
                }


            }

        }
        data[33][3]=String.valueOf((int)(Integer.valueOf(data[5][3])*0.3+Integer.valueOf(data[12][3])*0.2+Integer.valueOf(data[19][3])*0.5-Integer.valueOf(data[26][3])));
        data[62][3]=String.valueOf((int)(Integer.valueOf(data[42][3])*0.8+Integer.valueOf(data[49][3])*0.2-Integer.valueOf(data[56][3])));

        //添加表格
        Table table = sec.addTable(true);
        table.resetCells(68, 4);

        //设置表格第一行作为表头，写入表头数组内容，并格式化表头数据
        TableRow row = table.getRows().get(0);
//        row.isHeader(true);
        row.setHeight(30);
        table.applyHorizontalMerge(0, 0, 3);

        row.setHeightType(TableRowHeightType.Exactly);
//        row.getRowFormat().setBackColor(Color.ORANGE);

        row.getCells().get(0).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
        Paragraph p = row.getCells().get(0).addParagraph();
        p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        TextRange range1 = p.appendText("互联网金融与信息工程学院综合测评表");
        range1.getCharacterFormat().setFontName("Arial");
        range1.getCharacterFormat().setFontSize(12f);
        range1.getCharacterFormat().setBold(true);
        range1.getCharacterFormat().setTextColor(Color.black);


        //写入剩余组内容到表格，并格式化数据
        for (int r = 0; r < 64; r++) {
            TableRow dataRow = table.getRows().get(r + 1);
            dataRow.setHeight(20);
            dataRow.setHeightType(TableRowHeightType.Exactly);
//            dataRow.getRowFormat().setBackColor(Color.black);
            for (int c = 0; c < 4; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                range2.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }

        //纵向合并指定单元格
//        table.applyVerticalMerge(0,1,2);
//        table.applyVerticalMerge(0,4,5);
        table.applyHorizontalMerge(1, 0, 1);
        table.applyVerticalMerge(0, 2, 37);
        table.applyVerticalMerge(2, 3, 37);
        table.applyVerticalMerge(1, 3, 9);
        table.applyVerticalMerge(1, 10, 16);
        table.applyVerticalMerge(1, 17, 23);
        table.applyVerticalMerge(1, 24, 30);
        table.applyVerticalMerge(1, 31, 37);

//        table.getRows().get(2).getCells().get(0).setWidth(20f);

        table.applyVerticalMerge(3, 3, 9);
        table.applyVerticalMerge(3, 10, 16);
        table.applyVerticalMerge(3, 17, 23);
        table.applyVerticalMerge(3, 24, 30);
        table.applyVerticalMerge(3, 31, 37);
        table.applyHorizontalMerge(38, 0, 3);

        table.applyVerticalMerge(1, 40, 46);
        table.applyVerticalMerge(1, 47, 53);
        table.applyVerticalMerge(1, 54, 60);
        table.applyVerticalMerge(1, 61, 67);

        table.applyVerticalMerge(3, 40, 46);
        table.applyVerticalMerge(3, 47, 53);
        table.applyVerticalMerge(3, 54, 60);
        table.applyVerticalMerge(3, 61, 67);

        table.applyVerticalMerge(0, 39, 67);
        table.applyVerticalMerge(2, 40, 67);

        //插入图片到指定单元格
//        DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");
//        dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //设置单元格背景颜色
        /*for (int j = 1; j < table.getRows().getCount(); j++) {
            if (j % 2 == 0) {
                TableRow row2 = table.getRows().get(j);
                for (int f = 1; f < row2.getCells().getCount(); f++) {
                    row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144));
                }
            }
        }*/

        //设置表格边框样式
        table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);

        //保存文档
        Random random = new Random();
        int u = random.nextInt(10);
        doc.saveToFile("CreateTable" + u + ".docx", FileFormat.Docx_2013);


    }

    public static Map<String, String> mapStringToMap(String str) {
        str = str.substring(1, str.length() - 1);
        String[] strs = str.split(",");
        Map<String, String> map = new HashMap<String, String>();
        for (String string : strs) {
            String key = string.split("=")[0];
            String value = string.split("=")[1];
            // 去掉头部空格
            String key1 = key.trim();
            String value1 = value.trim();
            map.put(key1, value1);
        }
        return map;
    }
}
